package cn.bird.storm.validated.paramcheck.inter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public interface ICheckModel<T> {

    List<ICheckBean<T>> getCheckBeans();

    default String getStartStr(){
        return "";
    }

    default String getEndStr(){
        return System.lineSeparator();
    }
    /**
     * 放入检验bean
     */
    void setCheckBeans(List<ICheckBean<T>> checkBeans);

    default String check() {
        if (CollUtil.isNotEmpty(getCheckBeans())) {
            String result = getCheckBeans().stream()
                    .filter(Objects::nonNull)
                    .filter(ICheckBean::needPoint)
                    .map(ICheckBean::getMsg)
                    .filter(StrUtil::isNotBlank)
                    .collect(Collectors.joining(","));
            if (StrUtil.isNotBlank(result)) {
                return Optional.ofNullable(getStartStr()).orElse("")
                        + result
                        + Optional.ofNullable(getEndStr()).orElse("");
            }
        }
        return "";
    }

}
